<?php

namespace app\serverorder\service\order;
use app\manage\service\ApiBase;

/**
 * @author:      李颖
 * @DateTime:    2017-10-13 11:30:32
 * @Description: 订单模块
 */

class Order extends ApiBase
{
	
	protected $orderLogic = 'app\serverorder\logic\Order';
    protected $orderValidate = 'app\serverorder\validate\Order';


    /*创建订单 - 一键购买*/
    public function oneClickBuy(): array 
    {
    	$data = $this->haveIssetReceiveData(['user_id'=>0, 'goods_info'=>[], 'number'=>0, 'order_code'=>' ', 'goods_attrs'=>[], 'final_price'=>0, 'final_currency_price'=>0]);
    	/*检验数据*/
    	$validate = validate($this->orderValidate);
        if(!$validate->check($data)){ return verify_err(null, $validate->getError()); }
        if(!$validate->scene('checkGoodsInfo')->check($data['goods_info'])){ return verify_err(null, $validate->getError()); }
        $checkGoodsAttrs = $this->_checkGoodsAttrs($data['goods_attrs']);
        if($checkGoodsAttrs['status'] != SUCCESS_STATUS){ return $checkGoodsAttrs; }
        return $this->logic($this->orderLogic)->oneClickBuy($data);
    }

    //创建订单时 检验商品属性的数据
    protected function _checkGoodsAttrs($data): array 
    {
    	foreach ($data as $k => $v) {
    		if(!isset($v['name'], $v['en_name'], $v['value'])){
    			return verify_err(null, '请检查商品属性[' . $v['name'] . ']的参数传入');
    		}
    	}
    	return auto_success($data, '商品属性的参数完整');
    }

    //获取我的订单详情
    public function orderInfo(): array 
    {
        $data = $this->haveIssetReceiveData(['user_id'=>0,'id'=>0]);
        return $this->stepRun($data, 'orderInfo');
    }

    /*购物车结算*/
    public function settlement(): array 
    {
        $data = $this->haveIssetReceiveData(['user_id'=>0, 'order_code'=>' ', 'cartList'=>[]]);
        //判断基本数据格式
        $validate = validate($this->orderValidate);
        if(!$validate->check($data)){  return verify_err(null, $validate->getError()); }
        $checkCartList = $this->_checkCartList($data);
        if($checkCartList['status'] != SUCCESS_STATUS){ return $checkCartList; }
        return $this->stepRun($data, 'settlement');
    }

    /*检验购物车列表数据*/
    protected function _checkCartList($data): array 
    {
        //检验购物车数据的完整性
        foreach ($data['cartList'] as $k => $v) {
            if(!isset($v['user_id'], $v['goods_id'], $v['goods_info'], $v['goods_info']['title'], $v['goods_info']['price'], $v['goods_info']['orther_price']['currency'], $v['goods_info']['choose'], $v['goods_info']['profile_pic'], $v['goods_info']['product_code'], $v['price'], $v['orther_price']['currency'], $v['number'])){
                return verify_err(null, '购物车参数不完整');
            }
            if($v['user_id'] != $data['user_id']){
                return verify_err(null, '错误！购物车中商品【' . $v['goods_info']['title'] . '】不属于您');
            }
        }
        return auto_success($data, '购物车数据正常');
    }

    /*获取我的订单列表*/
    public function myOrderList(): array 
    {
        $data = $this->haveIssetReceiveData(['user_id'=>0, 'status'=>'', 'time'=>'', 'keyword'=>'', 'page'=>1, 'listRows'=>15], []);
        return $this->stepRun($data, 'myOrderList', 'myOrderList');
    }

    /*取消我的订单*/
    public function cancelMyOrder(): array 
    {
        $data = $this->haveIssetReceiveData(['user_id'=>0, 'id'=>0]);
        return $this->stepRun($data, 'cancelMyOrder');
    }

    /*删除我的订单*/
    public function delMyOrder(): array 
    {
        $data = $this->haveIssetReceiveData(['user_id'=>0, 'id'=>0]);
        return $this->stepRun($data, 'delMyOrder');
    }

    /*我的订单 - 去支付 仅：修改状态为已支付*/
    public function payOrder(): array 
    {
        $data = $this->haveIssetReceiveData(['id'=>0, 'user_id'=>0, 'integral_deduction'=>0, 'integral_deduction_money'=>0, 'integral_deduction_currency'=>0, 'pay_method'=>' ']);
        return $this->stepRun($data, 'payOrder');
    }

    /*后台获取所有订单分页列表*/
    public function allOrderList(): array 
    {
        $data = $this->haveIssetReceiveData(['page'=>1, 'listRows'=>15, 'keywords'=>'']);
        return $this->stepRun($data, 'allOrderList');
    }

    /*后台获取某订单详情*/
    public function adminOrderInfo(): array 
    {
        $data = $this->haveIssetReceiveData(['id'=>0]);
        return $this->stepRun($data, 'adminOrderInfo');
    }

    /*获取我的待付款数额*/
    public function myObligation(): array 
    {
        $data = $this->haveIssetReceiveData(['user_id'=>0]);
        return $this->stepRun($data, 'myObligation');
    }

    /*未付款的订单自动关闭*/
    public function orderAutoClose(): array 
    {
        $data = $this->haveIssetReceiveData(['order_auto_close'=>72]);
        return $this->stepRun($data, 'orderAutoClose');
    }
    //一周新增订单/总订单数量
    public function orderIncreTendency(): array 
    {
        $data = $this->haveIssetReceiveData(['dbDateFormat'=>'%y-%m-%d', 'dateFormat'=>'y-m-d']);
        return $this->stepRun($data, 'orderIncreTendency');
    }


    

}